<?php
/* Biogon
*  Author: ericfish
*  File: /admin/edit.php
*  Usage: Edit Post & Create New Post
*  Format: 1 tab indent(4 spaces), LF, GB2312, no-BOM
*
*  Subversion Keywords:
*
*  $Id$
*  $LastChangedDate$
*  $LastChangedRevision$
*  $LastChangedBy$
*  $URL$
*/
require_once('../gc-config.php');
require_once('../gc-settings.php');

auth_redirect();

// edit page init
if(isset($_REQUEST['q'])) {
	$post_id=$_REQUEST['q'];
	global $gcdb;
	$request = "SELECT a.post_title,a.post_date,a.post_content,a.show_in_home,a.comment_status,a.ping_status,b.photo_url FROM $gcdb->posts a LEFT JOIN $gcdb->photos b ON a.ID = b.post_ID WHERE a.ID = $post_id";
	$post = $gcdb->get_row($request);
	$post_title = $post->post_title;
	$post_date = $post->post_date;
	$post_content = $post->post_content;
	$photo_url = $post->photo_url;
	$post_tags = getTagsText($post_id);
	if($post->show_in_home == 'yes')
		$is_show = true;
	else
		$is_show = false;
	if($post->comment_status == 'open')
		$allow_comment = true;
	else
		$allow_comment = false;
	if($post->ping_status == 'open')
		$home_hide = true;
	else
		$home_hide = false;
}
// create new page init
else {
	$postArray = &$HTTP_POST_VARS;
	if(isset($postArray['new-page-title']))
		$post_title = $postArray['new-page-title'];
	else
		$post_title = "";
	$post_content = "";
	$post_date = date("Y-m-j G:i:s");
	$post_id = getNextId();
	$post_tags = "";
	$is_show = true;
	$allow_comment = true;
	$home_hide = true;
	$photo_url = "";
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Edit Post</TITLE>
<META http-equiv=Content-Type content="text/html; charset=<?php get_blog_charset();?>">
<LINK href="style/style.css" rel=stylesheet>

<!-- tinyMCE -->
<script language="javascript" type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
// Notice: The simple theme does not use all options some of them are limited to the advanced theme
tinyMCE.init({
theme : "advanced",
mode : "exact",
elements : "EditorAccessibility",
save_callback : "customSave",
//handle_event_callback: "ctlent",
// mode : "textareas",
width : "100%",
theme_advanced_buttons1 : "bold, italic, strikethrough, separator, bullist, numlist, outdent, indent, separator, justifyleft, justifycenter, justifyright ,separator, link, unlink, image, wordpress, separator, undo, redo, separator,forecolor,backcolor, separator, code",
theme_advanced_buttons2 : "",
theme_advanced_buttons3 : "",
paste_use_dialog : false,
theme_advanced_resizing : true,
theme_advanced_resize_horizontal : false,
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
dialog_type : "modal",
entity_encoding : "raw",
relative_urls : false,
remove_script_host : false,
force_p_newlines : true,
force_br_newlines : false,
convert_newlines_to_brs : false,
remove_linebreaks : true
});

// Custom save callback, gets called when the contents is to be submitted
function customSave(id, content) {
// alert(id + "=" + content);
}

function swap(tag){
	tagbox = document.getElementById('post_tags');
	tagbox.value += tag+" ";
	focusTo(tagbox);
}

// focus the caret to end of a form input (+ optionally select some text)
var range=0 //ie
function focusTo(obj, selectFrom) {
	if (typeof selectFrom == 'undefined') selectFrom = obj.value.length
	if(obj.createTextRange){ //ie + opera
		if (range == 0) range = obj.createTextRange()
		range.moveEnd("character",obj.value.length)
		range.moveStart("character",selectFrom)
		//obj.select()
		//range.select()
		setTimeout('range.select()', 10)
	} else if (obj.setSelectionRange){ //ff
		obj.select()
		obj.setSelectionRange(selectFrom,obj.value.length)
	} else { //safari :(
	 obj.blur()
}}

function toggle(objId) {
  var obj = document.getElementById(objId);
  if (!obj)
    return false;
  if (obj.style.display != 'none') {
    obj.style.display = 'none';
    return false;
  } else {
    obj.style.display = 'inline';
    return true;
  }
}
</script>
<!-- end: tinyMCE -->

</HEAD>
<BODY>
<form id="postForm" action="save.php" method="post">
<TABLE width="100%">
  <TBODY>
  <TR>
    <TD style="VERTICAL-ALIGN: top">
      <TABLE cellSpacing=0 cellPadding=0 width="100%">
        <TBODY>
        <TR>
          <TD class="tr_enclosure tr_tl"></TD>
          <TD class=tr_enclosure>
            <TABLE class=tr_option-bar>
              <TBODY>
              <TR>
                <TD><?php getNavBar(); ?></TD></TR></TBODY></TABLE></TD>
          <TD class="tr_enclosure tr_tr"></TD></TR>
        <TR>
          <TD style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px" colSpan=2>
            <DIV id=tr_grid-view>
            <TABLE class=tr_sort-bar>
              <THEAD>
              <TR id=tr_new-page-2-list-view>
                  <TD>
                  
                  Title: <INPUT id="post_title" name="post_title" size="40" value="<?php echo($post_title)?>">
                 <input type="hidden" id="post_id" name="post_id" value='<?php echo($post_id)?>'>
                  </TD></TR></THEAD>
              <TBODY></TBODY></TABLE>
            </DIV>
            
            
            <DIV id=tr_list-view>
            
            <TABLE id=tr_list-view>
           
              <TR id=tr_list-view-sortRow>
                <TD>
				&nbsp;&nbsp;&nbsp;<span id="uploadlink" style="cursor: pointer;" onclick="toggle('uploadlinks');">Upload photo to => </span>
				<span id="uploadlinks" style="display:inline;">
					<a href="http://footbig.com/upload/photos" target="_blank">footbig</a> / 
					<a href="http://www.yupoo.com/photos/upload" target="_blank">yupoo</a> / 
					<a href="http://www.flickr.com/photos/upload/" target="_blank">flickr</a> / 
					<a href="http://www.lensbig.com/" target="_blank">lensbig</a>
				</span> <br/>
                
                &nbsp;&nbsp;&nbsp;Photo URL:
                <INPUT id="photo_url" name="photo_url" size="120" value="<?php echo($photo_url)?>">
                 
                </TD></TR>
                
<tr><td>
&nbsp;&nbsp;&nbsp;Photo Description: <br/>
<textarea rows="9" name="EditorAccessibility" id="EditorAccessibility" style="width: 100%; word-break: break-all"><?php echo($post_content); ?></textarea>
</td></tr>
			<TR id=tr_list-view-sortRow>
                <TD> &nbsp;&nbsp;&nbsp;Date:
			<INPUT id="post_date" name="post_date" size="30" value="<?php echo($post_date)?>">
                </TD></TR>
                
              <TR id=tr_list-view-sortRow>
                <TD>&nbsp;&nbsp;&nbsp;Tags:
                <INPUT id="post_tags" name="post_tags" size="70" autocomplete="off" value="<?php echo($post_tags);?>"><br/>
                &nbsp;&nbsp;&nbsp;<span id="taglink" style="cursor: pointer;" onclick="toggle('taglinks');">Display all tags => </span>
                <span id="taglinks" style="display:inline;">&nbsp;&nbsp;<?php get_photo_tags_edit();?></span>
                </TD></TR>
                </TABLE></DIV>
            </TD>
          <TD class=tr_enclosure></TD></TR>
        <TR>
          <TD class="tr_enclosure tr_bl">.</TD>
          <TD class=tr_enclosure>
          <DIV id=tr_select-view>
            <TABLE class=tr_option-bar>
              <TBODY>
              <TR>
                <TD>
                      &nbsp;<INPUT class=tr_submit name="btnSubmit" type=submit value="Publish">
                      <input type="checkbox" name="D1" value="draft">
                      &nbsp;save as draft&nbsp;</TD>
                <TD style="PADDING-RIGHT: 0px" align="right">
                <INPUT class=tr_submit name="btnBack" onclick="javascript:location.href='index.php'" type=button value="Discard"></TD></TR></TBODY></TABLE></DIV></TD>
          <TD class="tr_enclosure tr_br">.</TD></TR></TBODY></TABLE></TD>
    	<TD style="VERTICAL-ALIGN: top" width="1%">
      </TD></TR></TBODY></TABLE>
<DIV class=tr_footer>
<span class="tr_footer-text"><span style="font-size: 10px"><?php getFooterBar(); ?></span></span></DIV>
</form></BODY></HTML>
